/*
 * @Description: 配置组件
 * @Author: franktrue 807615827@qq.com
 * @Date: 2021-07-28 16:31:39
 * @LastEditTime: 2021-07-28 17:07:41
 */
package setting

import (
	"github.com/spf13/viper"
)

type Setting struct {
	vp *viper.Viper
}

// 配置初始化
func NewSetting() (*Setting, error) {
	vp := viper.New()

	// 对应config.yaml文件
	vp.SetConfigName("config")
	vp.SetConfigType("yaml")

	vp.AddConfigPath("configs/")
	err := vp.ReadInConfig()
	if err != nil {
		return nil, err
	}
	return &Setting{vp}, nil
}

// 将配置映射到struct
func (s *Setting) ReadSection(k string, v interface{}) error {
	return s.vp.UnmarshalKey(k, &v)
}
